package TopK;

import java.util.*;

/**
 * @author dx
 * @version 1.0
 * @date 2022/6/20 11:30
 * @description:
 */
public class qsort {
    public static void main(String[] args) {
        Scanner sr = new Scanner(System.in);
        String[]arr = sr.nextLine().toString().split(" ");
        int []test = new int[arr.length];
        for(int i = 0;i<test.length;i++){
            test[i] = Integer.parseInt(arr[i]);
        }
        int k = sr.nextInt();
        int []ans = new int[k];
        ans = new Solution().topkFrequent(test,k);
        for(int x:ans){
            System.out.println(x);
        }
    }
}
class Solution{
    public int[] topkFrequent(int[] nums,int k){
        Map<Integer,Integer> occurrences = new HashMap<>();
        //统计出现次数的频率
        for(int num:nums){
            occurrences.put(num,occurrences.getOrDefault(num,0)+1);
        }
        List<int[]> values = new ArrayList<int[]>();
        //把<元素，频次>放到List数组里面
        for(Map.Entry<Integer,Integer> entry :occurrences.entrySet()){
            int num = entry.getKey(),count = entry.getValue();
            values.add(new int[]{num,count});
        }

        //结果
        int[]ret = new int[k];
        qsort(values, 0, values.size() - 1, ret, 0, k);
        return ret;
    }
    public void qsort(List<int[]> values,int start,int end,int[]ret,int reIndex,int k){
        int picked = (int)(Math.random()*(end - start+1))+start;
        Collections.swap(values,picked,start);

        int pivot = values.get(start)[1];//
        int index = start;

        //分区，把大于pivot的放在前面，小值放在后面
        for(int i = start+1;i<=end;i++){
            if(values.get(i)[1] >= pivot){
                Collections.swap(values,index+1,i);
                index++;
            }
        }
        Collections.swap(values,start,index);
        if(k <= index-start){
            qsort(values,start,index-1,ret,reIndex,k);
        }else {
            for(int i = start;i<= index;i++){
                ret[reIndex++] = values.get(i)[0];
            }
            if(k > index - start+1){
                qsort(values,index+1,end,ret,reIndex,k-(index-start+1));
            }
        }
    }
}
